package Sort;

import javax.swing.*;
import java.util.Stack;

public class Sort {

    public static void insertSort(int[]array){
        for (int i = 1; i < array.length ; i++) {
            int tmp=array[i];
            int j = i-1;
            for (; j >=0 ; j--) {
                if(array[j]>tmp){
                    array[j+1]=array[j];
                }else {
                    array[j+1]=tmp;
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }

    public static void shellSort(int[]array){
        int gap=array.length;
        while(gap>1){
gap=gap/2;
shell(array,gap);
        }
    }

    private static void shell(int[]array,int gap){
        for (int i = gap; i <array.length ; i++) {
            int j=i-gap;
            int tmp=array[i];
            for (;j>=0;j=j-gap){
                if(tmp<array[j]){
                    array[j+gap]=array[j];
                }else {
                    array[j+gap]=tmp;
                    break;
                }
            }
            array[j+gap]=tmp;
        }
    }


    public static void selectSort1(int[]array){
        for (int i = 0; i < array.length ; i++) {
            int max=0;
            int j = 0;
            for (; j <array.length-i-1 ; j++) {
                if(array[j]>array[max]){
                    max=j;
                }
            }
            swap(array,j,max);
        }
    }

    public static void selectSort2(int[]array){
       int left=0;
       int right= array.length-1;
       while(left<right){
           int max=left;
           int min=left;
           for (int i = left; i <=right ; i++) {
               if(array[i]>=array[max]){
                   max=i;
               }
               if (array[i]<array[min]){
                   min=i;
               }
           }
           swap(array,left,min);
           swap(array,right,max);
           left++;
           right--;
       }
    }


    private static void swap(int[]array,int i,int j){
        int tmp=array[i];
        array[i]=array[j];
        array[j]=tmp;
    }

    public static void heapSort(int[]array){
        create(array);
        int end=array.length-1;
        while(end>0){
            swap(array,0,end);
            sifDown(array,0,end);
            end--;
        }
    }
    
    private static void create(int[]array){
        for (int parent = (array.length-2)/2; parent >=0 ; parent--) {
            sifDown(array,parent,array.length);
        }
    }


    private static void sifDown(int[]array,int parent,int end){
        int child=parent*2+1;
        while(child<end){
            if(child+1<end&&array[child+1]>array[child]){
                child++;
            }
            if(array[parent]<array[child]){
                swap(array,parent,child);
                parent =child;
               child=child*2+1;
            }else {
                break;
            }
        }
    }


    public static void bubbleSort(int[]array){
        for (int i = 0; i < array.length-1 ; i++) {
            boolean flag=true;
            for (int j = 0; j < array.length-1-i ; j++) {
                if(array[j]>array[j+1]){
                    swap(array,j,j+1);
                    flag=false;
                }
            }
            if(flag){
                break;
            }
        }
    }



    public static void quickSort(int[]array){
        quick1(array,0,array.length-1);
    }

    private static void quick(int[]array,int left,int right){
       if(left>=right){
           return;
       }
       if(right-left<10){
           insertSortRange(array,left,right);
           return;
       }
       int mid=getMidNum(array,left,right);
       swap(array,mid,left);
       int pivot=partition3(array,left,right);
quick(array,left,pivot-1);
quick(array,pivot+1,right);
    }

    //快排非递归

    private static void quick1(int[]array,int left,int right){
        if(left>=right){
            return;
        }
        Stack<Integer>st=new Stack<>();
        st.push(left);
        st.push(right);
while (!st.empty()){
    int r=st.pop();
    int l=st.pop();
    if(r-l<=1){
       continue;
    }
   int d= partition(array,l,r);

    st.push(l);
    st.push(d);
    st.push(d+1);
    st.push(r);
}
    }

    private static void insertSortRange(int[]array,int left,int right){
        int tmp=array[left];
        int i=left+1;
        for (; i <=right ; i++) {
            int j = i-1;
            for (; j >=left ; j--) {
                if(array[j]>tmp){
                    array[j+1]=array[j];
                }else {
                    array[j+1]=tmp;
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }

    private static int getMidNum(int[]array,int left,int right){
        int mid=(left+right)/2;
        if(array[left]<array[right]){
            if(array[mid]<array[left]){
                return left;
            }else if(array[mid]>array[right]){
                return right;
            }else {
                return mid;
            }
        }else {
            if(array[mid]<array[right]){
                return right;
            } else if (array[mid]>array[left]) {
                return left;
            }else {
                return mid;
            }
        }
    }


    private static int partition(int[]array,int left,int right){
        int tmp=array[left];
        while (left<right){
            while (left<right&&array[right]>=tmp){
                right--;
            }
            array[left]=array[right];
            while (left<right&&array[left]<=tmp){
                left++;
            }
            array[right]=array[left];
        }
array[left]=tmp;
        return left;
    }

   private static int partitionHoare(int[]array,int left,int right){
        int tmp=array[left];
        int tmpLeft=left;
        while(left<right){
            while(left<right&&array[right]>tmp){
                right--;
            }
            while (left<right&&array[left]<tmp){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,tmpLeft,left);
        return left;
   }

   private static int partition2(int[]array,int left,int right){
        int prv=left;
        int cur=left+1;
        while(cur<=right){
            if(array[cur]<array[left]&&array[++prv]!=array[cur]){
                swap(array,prv,cur);
            }
            cur++;
        }
swap(array,left,prv);
        return prv;
   }


   private static int partition3(int[]array,int left,int right){
        int d=left+1;
        int pivod=array[left];
       for (int i = left+1; i <array.length ; i++) {
           if(array[i]<pivod){
               swap(array,d,i);
               d++;
           }
       }
       swap(array,d-1,left);
       return d-1;
   }


   //归并排序
    public static void mergeSort(int[]array){
        mergeSortTmp(array,0,array.length-1);
    }

    private static void mergeSortTmp(int[]array,int left,int right){
if(left>=right){
    return;
}
int mid=(left+right)/2;
mergeSortTmp(array,left,mid);
mergeSortTmp(array,mid+1,right);

merge(array,left,mid,right);
    }
    
    private static void merge(int[]array,int left,int mid,int right){
        int k=0;
        int[]tmp=new int[right-left+1];
        int s1=left;
        int s2=mid+1;
        while (s1<=mid&&s2<=right){
            if(array[s1]<array[s2]){
                tmp[k++]=array[s1++];
            }else {
                tmp[k++]=array[s2++];
            }
        }
        while(s1<=mid){
            tmp[k++]=array[s1++];
        }
        while (s2<=right){
            tmp[k++]=array[s2++];
        }
        for (int i = 0; i <k; i++) {
            array[left+i]=tmp[i];
        }
    }


}
